package com.yhj.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhj.reggie.common.R;
import com.yhj.reggie.entity.Employee;
import com.yhj.reggie.entity.Page;
import com.yhj.reggie.exception.BusinessException;
import com.yhj.reggie.mapper.EmployeeMapper;
import com.yhj.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;
    /**
     * 登录业务方法
     *
     * @param employee 传入的用户信息
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
        //获取用户对象
        Employee loginEmployee = employeeMapper.login(employee);
        //判断是否存在
        if (loginEmployee == null){
            throw new BusinessException("用户名或密码错误");
        }
        //判断密码是否正确
        //获取加密密文,
        String md5password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //判断密文和数据库密文是否一致
        if (!loginEmployee.getPassword().equals(md5password)){
            throw new BusinessException("用户名或密码错误");
        }
        //判断用户是否启用
        if (loginEmployee.getStatus()==0){
            throw new BusinessException("用户已被禁用,请联系管理员处理");
        }
        //以上都通过,调用成功方法构造返回数据,传入用户数据
        return R.success(loginEmployee);
    }


    /**
     * 添加员工方法
     *
     * @param employee
     * @return
     */
    @Override
    public boolean save(Employee employee) {
        Employee login = employeeMapper.login(employee);

        if (login !=null){
            throw new BusinessException("用户已存在");
        }
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //状态
        employee.setStatus(1);
        //设置创建和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.saveEmployee(employee);
        return true;
    }

    /**
     * 查找页面
     *
     * @param page     页码
     * @param pageSize 页码大小
     * @param name     查询的名字
     * @return 返回(员工)页面
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //设置分页查询第几页和每页几条
        PageHelper.startPage(page,pageSize);
        //执行查询所有数据的语句
        List<Employee> employeeList = employeeMapper.findEmployeeByPage(name);
        //使用pageInfo封装上面的结果
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        Page<Employee> objectPage = new Page<>();
        objectPage.setRecords(pageInfo.getList());
        objectPage.setTotal(pageInfo.getTotal());

        return objectPage;
    }

    /**
     * 修改员工
     *
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {

        employee.setUpdateTime(LocalDateTime.now());

        return employeeMapper.update(employee)>0;
    }

    /**
     * 根据用户id查找用户
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return R.success(employee);
    }
}
